<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
             :close-on-press-escape='false' :fullscreen='true'>
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
             
                <el-form-item label="任务号" prop="taskkey">
            <el-input v-model="dataForm.taskkey" placeholder="任务号"></el-input>
          </el-form-item>
          <el-form-item label="任务类型" prop="tasktype">
            <el-input v-model="dataForm.tasktype" placeholder="任务类型"></el-input>
          </el-form-item>
          <el-form-item label="优先级" prop="priority">
            <el-input v-model="dataForm.priority" placeholder="优先级"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-input v-model="dataForm.status" placeholder="状态"></el-input>
          </el-form-item>
          <el-form-item label="物料编码" prop="sku">
            <el-input v-model="dataForm.sku" placeholder="物料编码"></el-input>
          </el-form-item>
          <el-form-item label="数量" prop="qty">
            <el-input v-model="dataForm.qty" placeholder="数量"></el-input>
          </el-form-item>
          <el-form-item label="来源单类型" prop="sourcetype">
            <el-input v-model="dataForm.sourcetype" placeholder="来源单类型"></el-input>
          </el-form-item>
          <el-form-item label="来源单号" prop="sourcekey">
            <el-input v-model="dataForm.sourcekey" placeholder="来源单号"></el-input>
          </el-form-item>
          <el-form-item label="来源单行号" prop="sourceline">
            <el-input v-model="dataForm.sourceline" placeholder="来源单行号"></el-input>
          </el-form-item>
          <el-form-item label="来源单记录行号" prop="sourcelinelist">
            <el-input v-model="dataForm.sourcelinelist" placeholder="来源单记录行号"></el-input>
          </el-form-item>
          <el-form-item label="自库位" prop="fromlocation">
            <el-input v-model="dataForm.fromlocation" placeholder="自库位"></el-input>
          </el-form-item>
          <el-form-item label="至库位" prop="tolocation">
            <el-input v-model="dataForm.tolocation" placeholder="至库位"></el-input>
          </el-form-item>
          <el-form-item label="自箱" prop="fromtray">
            <el-input v-model="dataForm.fromtray" placeholder="自箱"></el-input>
          </el-form-item>
          <el-form-item label="至箱" prop="totray">
            <el-input v-model="dataForm.totray" placeholder="至箱"></el-input>
          </el-form-item>
          <el-form-item label="自格" prop="frombox">
            <el-input v-model="dataForm.frombox" placeholder="自格"></el-input>
          </el-form-item>
          <el-form-item label="至格" prop="tobox">
            <el-input v-model="dataForm.tobox" placeholder="至格"></el-input>
          </el-form-item>
          <el-form-item label="自批" prop="fromlot">
            <el-input v-model="dataForm.fromlot" placeholder="自批"></el-input>
          </el-form-item>
          <el-form-item label="至批" prop="tolot">
            <el-input v-model="dataForm.tolot" placeholder="至批"></el-input>
          </el-form-item>
          <el-form-item label="任务流水" prop="taskno">
            <el-input v-model="dataForm.taskno" placeholder="任务流水"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
          </el-form-item>
          <el-form-item label="任务下发标志" prop="sendflag">
            <el-input v-model="dataForm.sendflag" placeholder="任务下发标志"></el-input>
          </el-form-item>
          <el-form-item label="任务下发时间" prop="sendtime">
            <el-input v-model="dataForm.sendtime" placeholder="任务下发时间"></el-input>
          </el-form-item>
          <el-form-item label="任务接收时间" prop="receivetime">
            <el-input v-model="dataForm.receivetime" placeholder="任务接收时间"></el-input>
          </el-form-item>
          <el-form-item label="创建人" prop="adduser">
            <el-input v-model="dataForm.adduser" placeholder="创建人"></el-input>
          </el-form-item>
          <el-form-item label="创建时间" prop="addtime">
            <el-input v-model="dataForm.addtime" placeholder="创建时间"></el-input>
          </el-form-item>
          <el-form-item label="修改人" prop="updatuser">
            <el-input v-model="dataForm.updatuser" placeholder="修改人"></el-input>
          </el-form-item>
          <el-form-item label="修改时间" prop="updatetime">
            <el-input v-model="dataForm.updatetime" placeholder="修改时间"></el-input>
          </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {ref, reactive, onMounted, nextTick, computed} from 'vue';
import {getTaskById,putTask,postTask} from "@/network/lswms/task/Task"
import {ElMessage} from 'element-plus'
import _ from 'lodash'


export default {
  name: "taskAddOrUpdate",
  emits: ["refreshDataList"],
  setup(props, {emit}) {
    const visible = ref(false);
    const dataForm = ref({
		          id: '',
          taskkey: '',
          tasktype: '',
          priority: '',
          status: '',
          sku: '',
          qty: '',
          sourcetype: '',
          sourcekey: '',
          sourceline: '',
          sourcelinelist: '',
          fromlocation: '',
          tolocation: '',
          fromtray: '',
          totray: '',
          frombox: '',
          tobox: '',
          fromlot: '',
          tolot: '',
          taskno: '',
          remark: '',
          sendflag: '',
          sendtime: '',
          receivetime: '',
          adduser: '',
          addtime: '',
          updatuser: '',
          updatetime: ''
    });


    const dataRule = reactive({   
                taskkey: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tasktype: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          priority: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          status: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sku: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          qty: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sourcetype: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sourcekey: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sourceline: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sourcelinelist: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlocation: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolocation: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromtray: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          totray: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          frombox: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tobox: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromlot: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tolot: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          taskno: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          remark: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sendflag: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sendtime: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          receivetime: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          adduser: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          addtime: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          updatuser: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          updatetime: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ]    })

    const dataFormRef = ref(null)

    const init = () => {
      visible.value = true;
      nextTick(function () {
        dataFormRef.value.resetFields();
        if (dataForm.value.id) {
		gettaskInfo();
		}
        
      })
    }

	const gettaskInfo =()=>{
		getTaskById(dataForm.value.id).then(function (res) {
		  if (res.code !== 0) {
		    return ElMessage.error(res.msg);
		  }
		  dataForm.value = _.assignIn({}, dataForm.value, res.data);
		}).catch((err) =>{
		});
	}

    // 表单提交
    const dataFormSubmitHandle = _.debounce(() => {
      dataFormRef.value.validate(function (valid) {
        if (!valid) {
          return false;
        }
		
        let qing = ()=> {
          return !dataForm.value.id ?  postTask(dataForm.value) : putTask(dataForm.value)
        }
        qing().then(function (res) {
          if (res.code !== 0) {
            return ElMessage.error(res.msg);
          }
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 500,
            onClose: function () {
              visible.value = false
              emit('refreshDataList')
            }
          })
        }).catch(function () {
        });

      })
    }, 1000, {'leading': true, 'trailing': false})

    return {
      visible,
      dataForm,
      dataRule,
      init,
      dataFormSubmitHandle,
      dataFormRef

    }
  },


}
</script>

<style scoped>

</style>